package com.forum.jspgenerator;

import java.util.LinkedList;
import java.util.List;

import com.forum.domain.Ucu;
import com.forum.domain.UcuDAO;
import com.forum.domain.User;

public class GenerateP {

	User user;
	UcuDAO ucudao;
	//index represent the groups already get
	private int index = 0;
	//the number of the concerned group
	private int count = 0;
	//the group to show each time
	private final int MAXCOUNT = 10;
	
	List<User> userlist = new LinkedList();
	List<User> returnlist = new LinkedList();
	List<Ucu> uculist = new LinkedList();

	public GenerateP(User user) {
		this.user = user;
	}

	public List<User> getUser() {
		generateUsers();
		return returnlist;
	}

	private void generateUsers() {
		uculist = ucudao.findByProperty("userByUserid",user);
		for (int i = 0; i < uculist.size(); i++) {
			Ucu ucu = uculist.get(i);
			userlist.add(ucu.getUserByFollowingid());
		}
		count = userlist.size();
		if(count >  MAXCOUNT)
		{
			for(int i = 0 ; i < MAXCOUNT ; i++)
				returnlist.add(userlist.get(i));
			index = MAXCOUNT;
		}
		else
		{
			returnlist = userlist;
			index = count;
		}
	}
	
	public void setUcudao(UcuDAO ucudao) {
		this.ucudao = ucudao;
	}

	public List<User> moreUsers()
	{
		if (index > count - 1)
			return null;
		else {
			returnlist.clear();
			int currentcount = 0;
			
			if(userlist.size() < index + MAXCOUNT + 1)
				currentcount = userlist.size()-index;
			else
				currentcount = MAXCOUNT;
			
			for (int i = 0; i < currentcount; i++) {
				returnlist.add(userlist.get(i+index));
			}
			index = index + currentcount;
			return returnlist;
		}
	}
}
